import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2024-03-02
 * Time: 15:42
 */
@WebServlet("/request")
public class Method_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 告诉浏览器，你拿到的数据是 html
        resp.setContentType("text/html");

        // 把得到的数据汇总到一个字符串，统一返回到页面上。
        StringBuilder splice = new StringBuilder();

        // 下面内容是在浏览器上按照 html 的方式来展示的，此时 \n 在 html 中并不是换行
        // 使用 <br> 标签表示换行
        splice.append(req.getProtocol());
        splice.append("<br>");
        splice.append(req.getMethod());
        splice.append("<br>");
        splice.append(req.getRequestURI());
        splice.append("<br>");
        splice.append(req.getContextPath());
        splice.append("<br>");
        splice.append(req.getQueryString());
        splice.append("<br>");
        splice.append("<br>");

        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            splice.append(header + ":" + req.getHeader(header));
            splice.append("<br>");
        }
        resp.getWriter().write(splice.toString());
    }
}
